<template>
  <el-card>
    <el-row :gutter="20" class="header">
      <el-col :span="7">
        <el-input placeholder="请输入用户昵称..." clearable v-model="queryForm.query"></el-input>
      </el-col>
      <el-button type="button" :icon="Search" @click="initUserList">搜索</el-button>
    </el-row>
    <el-table :data="tableData" stripe style="width: 100%">
      <el-table-column prop="id" label="#ID" width="80" />
      <el-table-column prop="userName" label="账号" width="200" />
      <el-table-column prop="password" label="密码" width="200" />
      <el-table-column prop="email" label="邮箱" width="200" />
      <el-table-column prop="name" label="名称" width="200" />
      <el-table-column prop="createTime" label="创建时间" width="200" />
      <el-table-column prop="action" label="操作" width="300">
        <template v-slot="scope">
          <el-button type="danger" @click="handleDelete(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
        v-model:currentPage="queryForm.pageNum"
        v-model:page-size="queryForm.pageSize"
        :page-sizes="[10, 20, 30, 40,50]"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
    />
  </el-card>

</template>

<script setup>
import {Search} from '@element-plus/icons-vue'
import {nextTick, reactive, ref} from 'vue'
import axios from "@/util/axios";
import {ElMessage, ElMessageBox} from "element-plus";

const queryForm=ref({
  query:'',
  pageNum:1,
  pageSize:10
})

const total=ref(0)
const tableData =ref([])
const initUserList=async()=>{
  const res=await axios.post("admin/list",queryForm.value);
  tableData.value=res.data.adminList;
  total.value=res.data.total;
}

initUserList();

const handleSizeChange = (pageSize) => {
  queryForm.value.pageNum=1;
  queryForm.value.pageSize=pageSize;
  initUserList();
}

const handleCurrentChange = (pageNum) => {
  queryForm.value.pageNum=pageNum;
  initUserList();
}

// 编辑
const handleEdit = (raw) => {
  dialogFormVisible.value = true
  nextTick(() => {
    ruleFormRef.value.resetFields()
    state.form = JSON.parse(JSON.stringify(raw))
  })
}


const handleDelete = (id) => {

  ElMessageBox.confirm(
      '您确定要删除这条记录吗?',
      '系统提示',
      {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      }
  )
      .then(async() => {
        console.log("id="+id)
        let res=await axios.get("admin/delete/"+id);
        if(res.data.code==0){
          ElMessage({
            type: 'success',
            message: '删除成功！',
          });
          initUserList();
        }else{
          ElMessage({
            type: 'error',
            message: res.data.msg,
          });
        }

      })
      .catch(() => {

      })
}

</script>

<style lang="scss" scoped>

.header{
  padding-bottom: 16px;
  box-sizing: border-box;
}

.el-pagination{
  padding-top: 15px;
  box-sizing: border-box;
}


</style>